//------------------------------------------------------------------------------
#include <QMessageBox>
#include "CComboResultFrm.h"
#include "CSelectTypeCommandeFrm.h"
#include "CMySQL.h"
//------------------------------------------------------------------------------
CSelectTypeCommandeFrm::CSelectTypeCommandeFrm(QWidget *parent) : QDialog(parent) {
	setupUi(this);
	devisSelected=false;
}
//------------------------------------------------------------------------------
void CSelectTypeCommandeFrm::onCombo(CMyComboBox *comboBox) {
	CComboResultFrm *comboResultFrm=new CComboResultFrm(comboBox->getTableRemp(), comboBox->getChampRemp(), this);
	
	comboResultFrm->setModal(true);
	comboResultFrm->exec();
	
	if(comboResultFrm->result() == QDialog::Accepted) {
		comboBox->setValue(comboResultFrm->getResult());
		infoDevis=QMap<QString, QString>(comboResultFrm->getResults());
		devis->setValue(infoDevis.value("teteDevis.nomDevis"));
	}
	
	delete comboResultFrm;
}
//------------------------------------------------------------------------------
void CSelectTypeCommandeFrm::on_optionSimple_clicked(bool checked) {
	devis->setEnabled(false);
	devisSelected=false;
}
//------------------------------------------------------------------------------
void CSelectTypeCommandeFrm::on_optionDevis_clicked(bool checked) {
	devis->setValue("");
	devis->setEnabled(true);
	devisSelected=true;
}
//------------------------------------------------------------------------------
void CSelectTypeCommandeFrm::accept(void) {
	if(optionDevis->isChecked()) {
		if(!CMySQL::getInstance()->existe("teteDevis", "nomDevis", devis->getValue())) {
			QMessageBox msgBox(	QMessageBox::Warning, QObject::tr("Erreur"), 
								tr("Le devis '")+devis->getValue()+tr("' n'existe pas, impossible de créer la commande"), QMessageBox::Ok);
			msgBox.exec();	
			
			return;
		}
	}
	
	QDialog::accept();
}
//------------------------------------------------------------------------------